<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 课程分类控制器
 */
class Source extends Backend
{
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = 'id,name';
    /**
     * 当前控制器下的一个模型属性
    */
    protected $model = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->SourceModel = model('Business.Source');
    }

    public function index($courseid = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SourceModel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->SourceModel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {

            $params = $this->request->post('row/a');
            if ($params) {
                $result = $this->SourceModel->validate("Common/Business/Source")->save($params);

                if ($result === false) {
                    $this->error($this->SourceModel->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->SourceModel->find($ids);

        if (!$row) {
            $this->error(__('未找到当前来源名称'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params['id'] = $ids;
                $result = $this->SourceModel->validate("Common/Business/Source")->isUpdate(true)->save($params);

                if ($result === false) {
                    $this->error($this->SourceModel->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->SourceModel->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('没有找到该来源'));
                exit;
            }
        }

        $result = $this->SourceModel->destroy($ids);

        if ($result === false) {
            $this->error($this->SourceModel->getError());
            exit;
        }

        $this->success();
        exit;

    }
}
